package com.icoolkj.ms.common.core.response;

import com.icoolkj.ms.common.core.exception.IcoolkjMSException;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/02 17:16
 */
@Slf4j
public class MapBean<K, V> extends ConcurrentHashMap<K, V> implements Serializable {

    public MapBean() {
    }

    // 静态工厂方法用于创建MapBean实例
    public static <K, V> MapBean<K, V> create() {
        return new MapBean<>();
    }

    // 添加泛型方法用于设置参数
    public <T> MapBean<K, V> withParameter(String key, T value) {
        put((K) key, (V) value);
        return this;
    }

    // 添加泛型方法用于获取参数
    public <T> T getParameter(String key, Class<T> targetType) {
        Object value = get(key);
        if (value != null && targetType.isInstance(value)) {
            return targetType.cast(value);
        } else {
            throw new IcoolkjMSException("Parameter not found or type mismatch for key: " + key);
        }
    }

    // 添加泛型方法支持默认值
    public <T> T getParameter(String key, Class<T> targetType, T defaultValue) {
        Object value = get(key);
        if (value != null && targetType.isInstance(value)) {
            return targetType.cast(value);
        } else {
            return defaultValue;
        }
    }
}
